//
//  DLCustomTitleView.m
//  delongzhitou
//
//  Created by Mr.Wang on 2018/11/22.
//  Copyright © 2018 delongzhitou. All rights reserved.
//

#import "DLCustomTitleView.h"
#define kSelfHeight 23
#define kDistanceTitleFromImageView 10

@interface DLCustomTitleView ()

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UILabel *titleLabel;

@end

@implementation DLCustomTitleView


- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self layoutSubcontrols];
    }
    return self;
}

- (void)layoutSubcontrols {
    [self addSubview:self.imageView];
    [self addSubview:self.titleLabel];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self);
        make.centerY.equalTo(self);
    }];
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.titleLabel);
        make.right.equalTo(self.titleLabel.mas_left).offset(-kDistanceTitleFromImageView);
    }];
    self.imageTintColor = [UIColor whiteColor];
}

- (void)updateSelfSize {
    CGFloat titleWidth = 0;
    if (!isEmpty(self.title)) {
        titleWidth = [self.title sizeWithAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:kAutoWidth(17)]}].width;
    }
    CGFloat imageWidth = 0;
    if (self.imageView.image) {
        imageWidth = self.imageView.image.size.width + kDistanceTitleFromImageView;
    }
    
    CGFloat selfWidth = titleWidth + imageWidth;
    self.size = CGSizeMake(selfWidth, kSelfHeight);
    
    if (0 == imageWidth && titleWidth > 0) {
        // 只有title
        [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
        }];
    } else if (imageWidth > 0 && 0 == titleWidth) {
        // 只有image
        [self.imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
        }];
    } else if (imageWidth > 0 && titleWidth > 0) {
        // image和title都有
        CGFloat titleCenterX = 0;
        if (self.imageView.image) {
            titleCenterX += (kDistanceTitleFromImageView + self.imageView.image.size.width) * 0.5;
        }
        [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self).offset(titleCenterX);
        }];
    }
}

#pragma mark - SetterGetter

- (void)setTitle:(NSString *)title {
    _title = title;
    self.titleLabel.text = title;
    [self updateSelfSize];
}

- (void)setImageName:(NSString *)imageName {
    _imageName = imageName;
    if (isEmpty(imageName)) return;
    
    if ([imageName hasPrefix:@"http"]) {
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:imageName]];
    } else {
        UIImage *image = [UIImage imageNamed:imageName];
        image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        self.imageView.image = image;
        self.imageView.tintColor = self.imageTintColor;
    }
    [self updateSelfSize];
}

- (void)setImageTintColor:(UIColor *)imageTintColor {
    _imageTintColor = imageTintColor;
    if (_imageName && imageTintColor) {
        UIImage *image = [UIImage imageNamed:self.imageName];
        image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        self.imageView.image = image;
        self.imageView.tintColor = imageTintColor;
    }
}

- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
    }
    return _imageView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:kAutoWidth(16)];
    }
    return _titleLabel;
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
